package com.cp.order.interceptor;

import javax.servlet.http.HttpServletRequest;

import com.cp.order.model.SysUser;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

public class AuthInteceptor implements Interceptor {

	public void intercept(Invocation ai) {
		Controller controller = ai.getController();
		// 设置访问绝对路径
		HttpServletRequest request = controller.getRequest();
		String contextPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
				+ request.getContextPath();

		controller.setAttr("ctx", contextPath);
		controller.setSessionAttr("ctx", request.getContextPath());

		// 验证权限
		SysUser user = controller.getSessionAttr("userSession");
		if (user == null) {
			ai.getController().redirect("/");
			return;
		} else {
			controller.setAttr("user", user);
		}

		try {
			ai.invoke();
		} catch (Exception e) {
			// doLog(ai, e);
			throw e;
		}
	}

}
